﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="report_score_default.aspx.cs" Inherits="SQT.tracnghiem.Ajax.ASP.Class.report_score" %>

<%@ Import Namespace="DataAccess" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <%
        int id = int.Parse(Request.QueryString["id"]);
        var get_student = new TblUserController().FetchAll().Where(x => x.ClassX == id);
        var get_semester = new TblSemesterController().FetchAll();
        var get_group = new TblExamGroupController().FetchAll().Where(x=>x.Semester==get_semester.First().SemesterId);
        var get_exam = new TblExamController().FetchAll().Where(x => x.ExamGroup == get_group.First().GroupId);
        int count = 0;
    %>
    <div class="w3-padding">
        <label class="w3-padding-right">Điểm số</label>
        <select class="choose w3-teal" id="ddl_report_semester">
            <%foreach (TblSemester semester in get_semester)
                { %>
            <option value="<%=semester.SemesterId %>"><%=semester.SemesterName %></option>
            <%} %>
        </select>
        <select class="choose w3-teal" id="ddl_report_group">
            <%foreach (TblExamGroup group in get_group)
                { %>
            <option value="<%=group.GroupId %>"><%=group.GroupName %></option>
            <%} %>
        </select>
        <a href="javascript:;" class="class-manager-btn w3-btn w3-teal" onclick="change_report_score()" title="Thống kê"><i class="fa fa-table"></i></a>
    </div>
    <div class="class-insert-point-table w3-responsive w3-padding-left w3-padding-right w3-padding-bottom">
        <%if (get_student.ToList().Count != 0)
            { %>
        <table class="w3-white">
            <tr class="">
                <td class="name-col view-name-col w3-deep-purple">Họ tên</td>
                <td rowspan="<%=get_student.ToList().Count+1 %>" class="middle-point-col">
                    <div id="class-point-slide" class="w3-display-container carousel slide" data-ride="carousel">
                        <div class="carousel-inner" role="listbox">
                            <%foreach (TblExam exam in get_exam)
                                { %>
                            <div class="item <%if (count == 0)
                                {%>active<%} %> w3-center">
                                <div class="w3-orange w3-text-white w3-padding-4"><%=exam.ExamName %></div>
                                <div class="w3-row">
                                    <div class="w3-col point-col w3-blue">Từ vựng</div>
                                    <div class="w3-col point-col w3-indigo">Ngữ pháp</div>
                                    <div class="w3-col point-col w3-purple">Nghe</div>
                                    <div class="w3-col point-col w3-green">Hán tự</div>
                                    <%--<div class="w3-col point-col w3-red">Giao tiếp</div>--%>
                                </div>
                                <%foreach (TblUser student in get_student)
                                    {
                                        float? write = null, grammar = null, listen = null, kanji = null;
                                        var get_score = new TblExamScoreController().FetchAll().Where(x => x.ScoreStudentId == student.UserId && x.ScoreExam == exam.ExamId).ToList();
                                        if (get_score.Count != 0)
                                        {
                                            if (get_score[0].ScoreListen != null)
                                                listen = (float)get_score[0].ScoreListen;
                                            if (get_score[0].ScoreGrammar != null)
                                                grammar = (float)get_score[0].ScoreGrammar;
                                            if (get_score[0].ScoreKanji != null)
                                                kanji = (float)get_score[0].ScoreKanji;
                                            //if (get_score[0].ScoreSpeak != null)
                                            //    speak = (float)get_score[0].ScoreSpeak;
                                            if (get_score[0].ScoreWrite != null)
                                                write = (float)get_score[0].ScoreWrite;
                                        }%>
                                <div class="w3-row">
                                    <%if (write != null)
                                        {%>
                                    <div class="w3-col point-col view-point-col"><%=write %></div>
                                    <%}
                                    else
                                    { %>
                                    <div class="w3-col point-col view-point-col">&nbsp;</div>
                                    <%} %>
                                    <%if (grammar != null)
                                        {%>
                                    <div class="w3-col point-col view-point-col"><%=grammar %></div>
                                    <%}
                                    else
                                    { %>
                                    <div class="w3-col point-col view-point-col">&nbsp;</div>
                                    <%} %>
                                    <%if (listen != null)
                                        {%>
                                    <div class="w3-col point-col view-point-col"><%=listen %></div>
                                    <%}
                                    else
                                    { %>
                                    <div class="w3-col point-col view-point-col">&nbsp;</div>
                                    <%} %>
                                    <%if (kanji != null)
                                        {%>
                                    <div class="w3-col point-col view-point-col"><%=kanji %></div>
                                    <%}
                                    else
                                    { %>
                                    <div class="w3-col point-col view-point-col">&nbsp;</div>
                                    <%} %>
                                    <%--<%if (speak != null)
                                        {%>
                                    <div class="w3-col point-col view-point-col"><%=speak %></div>
                                    <%}
                                    else
                                    { %>
                                    <div class="w3-col point-col view-point-col">&nbsp;</div>
                                    <%} %>--%>
                                </div>
                                <%} %>
                            </div>
                            <% count++;
                                } %>
                        </div>

                        <div class="class-point-slide-btn w3-display-topmiddle">
                            <a class="w3-text-white w3-left" href="#class-point-slide" role="button" data-slide="prev">
                                <i class="fa fa-chevron-left"></i>
                            </a>
                            <a class="w3-text-white w3-right" href="#class-point-slide" role="button" data-slide="next">
                                <i class="fa fa-chevron-right"></i>
                            </a>
                        </div>
                    </div>
                </td>
                <td class="average-col view-average-col w3-light-blue">Trung bình</td>
            </tr>
            <%foreach (TblUser student in get_student)
                {
                    decimal? average_exam = 0, average_total = 0;
                    foreach(TblExam exam in get_exam)
                    {
                        var get_score = new TblExamScoreController().FetchAll().Where(x => x.ScoreStudentId == student.UserId && x.ScoreExam == exam.ExamId);
                        if (get_score.ToList().Count == 0)
                        {
                            average_exam = null;
                            average_total = null;
                            break;
                        }
                        else
                        {
                            if (get_score.Single().ScoreWrite==null || get_score.Single().ScoreGrammar==null || get_score.Single().ScoreListen ==null || get_score.Single().ScoreKanji == null)
                            {
                                average_exam = null;
                                average_total = null;
                                break;
                            }
                            else
                            {
                                decimal? temp = get_score.Single().ScoreWrite + get_score.Single().ScoreGrammar + get_score.Single().ScoreListen + get_score.Single().ScoreKanji;

                                if (exam.ExamTotal == true)
                                    average_total = temp/decimal.Parse("4") * decimal.Parse("2");
                                else
                                    average_exam += temp;
                            }
                        }
                    }

                    
            %>
            <tr>
                <td class="name-col"><%=student.FullName %></td>
                <%if (average_exam != null && average_total!=null)
                    {
                        decimal? average = 0;
                        var count_exam = new TblExamController().FetchAll().Where(x => x.ExamGroup == get_group.First().GroupId && x.ExamTotal == false);
                        int count_temp = count_exam.ToList().Count * 4;
                        average = (average_exam/decimal.Parse(count_temp.ToString()) + average_total) / decimal.Parse("3");
                        %>
                <td class="average-col"><%=Math.Round(decimal.Parse(average.ToString()), 2) %></td>
                <%}
                        else
                        { %>
                <td class="average-col">&nbsp;</td>
                <%} %>
            </tr>
            <%} %>
        </table>
        <%} %>
    </div>
</body>
</html>
